package com.qdxwx.web.controller.live;


import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.live.LiveCourseService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.member.MemberType;
import com.qdxwx.web.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 直播接口
 * @author developer
 */
@Controller
@RequestMapping("/live")
public class LiveListController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(LiveListController.class);
	@Autowired
	private CourseService courseService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private LiveCourseService liveCourseService;
	@Autowired
	private MemberTypeService memberTypeService;
	@Autowired
	private WebsiteProfileService websiteProfileService;
	/**
	 * 课程直播列表展示,搜索课程
	 */
	@RequestMapping("/list.html")
	public String showLiveCourseList(HttpServletRequest request, ModelMap model) {

		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setTeacherId(TKConvert.toInteger(request.getParameter("teacherId")));
		courseQuery.setCategoryId(TKConvert.toInteger(request.getParameter("categoryId")));
		courseQuery.setOrderBy(TKConvert.toInteger(request.getParameter("orderBy")));
		courseQuery.setMemberTypeId(TKConvert.toInteger(request.getParameter("memberTypeId")));
		courseQuery.setIsFree(TKConvert.toInteger(request.getParameter("isFree")));
		courseQuery.setIsLiving(TKConvert.toInteger(request.getParameter("isLiving")));
		//只查询上架的
		courseQuery.setIsAvailable(1);
		String searchKey = TKString.searchKeyFilter(request.getParameter("searchKey"));
		if (searchKey.length()>0) {
			courseQuery.setCourseName(searchKey);
		}

		courseQuery.setSellType("LIVE");

		PageRequest page = new PageRequest();
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		page.setCurrentPage(currentPage);
		// 页面传来的数据放到page中
		page.setPageSize(8);

		int totalCount = liveCourseService.queryWebLiveCourseListPageCount(courseQuery);

		model.put("totalPages", page.getTotalPages(totalCount));
		// 搜索课程列表

		List<Course> courseList = liveCourseService.queryWebLiveCourseListPage(courseQuery, page);
		if (courseList.size() > 0) {
			for (Course course : courseList) {
				List<CourseNode> courseNodeList = liveCourseService.getNextByCourseId(course.getCourseId());
				course.setCourseNodeList(courseNodeList);
			}
		}
		model.put("courseList", courseList);

		// 查询所有1级专业
		List<Category> categoryRoots = categoryService.getChildren(0);
		Category category = categoryService.get(courseQuery.getCategoryId());

		/*获取专业筛选条件的信息*/
		model.put("category", category);

		//根据条件专业查询 所有的子专业
		if (ObjectUtils.isNotNull(courseQuery.getCategoryId())) {
			//查询子专业
			List<Category> categoryChildren;
			if (category.getParentId() != 0) {//条件为二级专业
				categoryChildren = categoryService.getChildren(category.getParentId());
				model.put("categoryParentId", category.getParentId());//父级id
				category = categoryService.get(category.getParentId());
				model.put("categoryParentName", category.getCategoryName());//父级 专业名
			} else {//条件为一级专业
				categoryChildren = categoryService.getChildren(category.getCategoryId());
				model.put("categoryParentName", category.getCategoryName());//当前查询专业名
			}
			model.put("categoryChildren", categoryChildren);
		}

		model.put("page", page);
		model.put("courseQuery", courseQuery);
		model.put("categoryRoots", categoryRoots);

		Map<String, Object> serviceSwitch = websiteProfileService.getMap(WebsiteProfileType.serviceSwitch.toString());
		if ("ON".equals(serviceSwitch.get("member")) && ObjectUtils.isNotNull(memberTypeService)) {
			/*会员类型集合*/
			List<MemberType> memberTypeList = memberTypeService.getList(new MemberType(), null);
			model.put("memberTypeList", memberTypeList);
		}
		/*用于移动端展示的专业列表 查询的所有专业*/
		List<Category> mobileCategories = categoryService.getList(new Category());
		/*用于移动端 查询选择的subject的父级*/
		/*用于移动端*/
		model.put("mobileCategories", mobileCategories);
		/*用于移动端*/
		model.put("categoryParent", category);
		model.put("currentTeacher", teacherService.get(courseQuery.getTeacherId()));

		return getViewPath("live/live-list");
	}
}